﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Consul.AspNetCore;
using ZhaoXi.MSACommerce.Consul.ServiceDiscovery;
using ZhaoXi.MSACommerce.Consul.ServiceRegistrantion;
using Zhaoxi.MSACommerce.HttpApi.Common.Infrastructure;
using Zhaoxi.MSACommerce.HttpApi.Common.Service;
using ZhaoXi.MSACommerce.Infrastructure.EnityFrameworkCore.interceptor;
using ZhaoXi.MSACommerce.UserCases.Common.Interfaces;

namespace Zhaoxi.MSACommerce.HttpApi.Common
{
    public static class DependencyInjection
    {
        public static IServiceCollection AddHttpApiCommon(this IServiceCollection services)
        {
            services.AddScoped<IUser, CurrentUser>();

            services.AddHttpContextAccessor();

            services.AddHealthChecks();

            services.AddExceptionHandler<UserCaseExceptionHandler>();

            services.AddProblemDetails();

            ConfigureCors(services);

            ConfigureSwagger(services);

            return services;
        }


        private static void ConfigureCors(IServiceCollection services)
        {
            //跨域请求
            services.AddCors(options =>
            {
                options.AddPolicy("AllowAny", builder =>
                {
                    builder.AllowAnyOrigin()
                        .AllowAnyMethod()
                        .AllowAnyHeader();
                });
            });
        }


        private static void ConfigureSwagger(IServiceCollection services)
        {
            services.AddEndpointsApiExplorer();

            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc(name: "v1", new OpenApiInfo
                {
                    Title = "电商平台 API 文档",
                    Version = "v1",
                    Description = "一个微服务架构的电商平台实战项目"
                });

                options.AddSecurityDefinition(name: "Bearer", new OpenApiSecurityScheme
                {
                    Name = "JWT Bearer 认证",
                    In = ParameterLocation.Header,
                    Type = SecuritySchemeType.Http,
                    Scheme = "Bearer",
                    BearerFormat = "JWT"
                });

                options.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Id = "Bearer",
                                Type = ReferenceType.SecurityScheme
                            }
                        },
                        new string[] { }
                    }
                });
            });
        }

        // private static void ConfigureConsul(IServiceCollection services, IConfiguration configuration)
        // {
        //     var configurationSection = configuration.GetSection(key: "ServiceCheck");
        //     var serviceCheck = configurationSection.Get<ServiceCheckConfiguration>();
        //     services.Configure<ServiceConfiguration>(configurationSection);
        //
        //     services.AddConsul();
        //     services.AddConsulService(serviceConfiguration =>
        //     {
        //         serviceConfiguration.ServiceAddress = new Uri(configuration["urls"] ?? configuration
        //             ["applicationUrl"]);
        //     }, serviceCheck);
        //
        //     services.AddConsulDiscovery();
        // }
    }
}